<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Defray extends Admin_Controller {

	function __construct ()
	{
		parent::__construct();
        $this->load->model('defray_m');
        $this->load->library('pagination');
		if(!$this->auth->is_admins()){
			redirect(site_url('user/login'));
		}
	}
	
	public function index($page=1)
	{
        $user_id = $this->session->userdata('user_id');
        $user = $this->user_m->get_user_by_user_id($user_id);
        $key=!empty($this->input->get('project_name'))?$this->input->get('project_name'):'';
        $limit = 30;
        $config['uri_segment'] = 4;
        $config['use_page_numbers'] = TRUE;
        $config['reuse_query_string'] = TRUE;
        if($user['user_group']==4){
            $config['total_rows'] = $this->defray_m->count_defray_find($key,$user_id);
        }else{
            $config['total_rows'] = $this->defray_m->count_defray_find($key);
        }
        $config['base_url'] = base_url('manage/defray/index/');

        $config['per_page'] = $limit;
        $config['num_links'] = 10;
        $start = ($page-1)*$limit;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links($key);
        if($user['user_group']==4){
            $data['defray'] = $this->defray_m->get_defray_list($start,$limit,$key,$user_id);
        }else{
            $data['defray'] = $this->defray_m->get_defray_list($start,$limit,$key);
        }
        $data['zhichu'] = $this->defray_m->count_shouzhi_defray(1);
        $data['shouru'] = $this->defray_m->count_shouzhi_defray(2);
        $data['key']=$key;
        $data['user_group']=$user['user_group'];
        $this->load->view('defray/index',$data);
	}
	//新增成员
    public function add(){
        $this->load->view('defray/form');
    }
    //更新信息
    public function update(){
        $post = $this->input->post();
        $defray = array(
            'defray_title'=>$post['defray_title'],
            'defray_time'=>$post['defray_time'],
            'defray_pic'=>$post['defray_pic'],
            'defray_money'=>$post['defray_money'],
            'user_id'=>$this->session->userdata('user_id'),
            'defray_add_time'=>time(),
            'defray_status'=>1,
        );
        $result = $this->defray_m->add_defray($defray);
        if($result){
            show_message('添加成功',site_url(X_URL.'defray/index'),1);
        }else{
            show_message('添加失败',site_url(X_URL.'defray/index'),1);
        }
    }
    public function agree($defray_id){
        if ($defray_id) {
            $charge = $this->defray_m->update_defray($defray_id,array('defray_status'=>2,'defray_end_time'=>time()));
        } else {
            show_message('参数错误',site_url(X_URL.'defray/index'),1);
        }
        if(!empty($charge)){
            show_message('操作成功',site_url(X_URL.'defray/index'),1);
        }else{
            show_message('操作失败',site_url(X_URL.'defray/index'),1);
        }
    }

    public function reject($defray_id){
        if ($defray_id) {
            $charge = $this->defray_m->update_defray($defray_id,array('defray_status'=>3,'defray_end_time'=>time()));
        } else {
            show_message('参数错误',site_url(X_URL.'defray/index'),1);
        }
        if(!empty($charge)){
            show_message('操作成功',site_url(X_URL.'defray/index'),1);
        }else{
            show_message('操作失败',site_url(X_URL.'defray/index'),1);
        }
    }
    //图片上传
    public function upload_pic(){
        $name = $this->input->get('name',TRUE);
        $uniqid = md5('shop_auth'.time().uniqid());
        $config['upload_path'] = FCPATH .'uploads/defray/';
        $config['allowed_types'] = 'gif|jpg|png|JPG|PNG|JPEG|GIF|bmp|jpeg';
        $config['file_name'] = $uniqid."_".$name.'.jpg';
        $config['encrypt_name'] = false;
        $config['overwrite'] = true;
        $config['max_size'] = 3000;
        if(!file_exists($config['upload_path'])){
            mkdir($config['upload_path'],0777,true);
        }
        $this->load->library('upload', $config);
        if(!$this->upload->do_upload(''.$name.'')) {
            $arr = array ('status'=>4,'msg'=>'图片上传失败');
        }else{
            $upload_data = $this->upload->data();
            $url = base_url('uploads/defray/'.$upload_data['file_name'].'?t='.time());
            $arr = array ('status'=>1,'msg'=>'图片上传成功','url'=>$url,"path"=>'uploads/defray/'.$upload_data['file_name']);
        }
        echo json_encode($arr);
    }


}